import { Controller, Get, Post, Body, Patch, Param, Res } from '@nestjs/common';
import { VisitService } from './visit.service';


@Controller('visit')
export class VisitController {
  constructor(private readonly visitService: VisitService) { }

  @Get()
  async visitweb(@Res() res) {
    const data = new Date(Date.now())
    const year = data.getFullYear()
    const month = data.getMonth() + 1
    const week = Math.ceil(data.getDay() / 7) + 1 == 5 ? 4 : Math.ceil(data.getDay() / 7) + 1
    const result2 = await this.visitService.findevist(year, month, week)
    console.log(year, month, week)

    if (!result2) {
      await this.visitService.visitweb(year, month, week)
      res.status(200).send({
        mesage: "ok"
      })
    } else {
      res.status(200).send({
        mesage: "ok"
      })
    }


  }

  @Get('/register')
  async register(@Res() res) {
    const data = new Date(Date.now())
    const year = data.getFullYear()
    const month = data.getMonth() + 1
    const week = Math.ceil(data.getDay() / 7) + 1 == 5 ? 4 : Math.ceil(data.getDay() / 7) + 1
    const result2 = await this.visitService.registerfind(year, month, week)
    if (!result2) {
      await this.visitService.register(year, month, week)
      res.status(200).send({
        mesage: "ok"
      })
    } else {
      res.status(200).send({
        mesage: "ok"
      })
    }
  }
  @Get('/register/:year/:month')
  async getDatevisite2(@Res() res, @Param() Param) {
    const result = await this.visitService.finddata(Number(Param.year), Number(Param.month))
    console.log(result)
    res.status(200).send(
      {
        message: "ok",
        status: 200,
        data: result

      }
    )
  }
  @Get('/admin/:year/:month')
  async getDatevisite(@Res() res, @Param() Param) {
    const result = await this.visitService.finddata(Number(Param.year), Number(Param.month))
    console.log(result)
    res.status(200).send(
      {
        message: "ok",
        status: 200,
        data: result

      }
    )
  }
}
